package com.ruoyi.basedata.service.impl;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.basedata.mapper.UsageRangeMapper;
import com.ruoyi.basedata.domain.UsageRange;
import com.ruoyi.basedata.service.IUsageRangeService;

/**
 * 用量区间设置Service业务层处理
 *
 * @author TsingYu
 * @date 2024-07-13
 */
@Service
public class UsageRangeServiceImpl extends ServiceImpl<UsageRangeMapper, UsageRange> implements IUsageRangeService {

    private final UsageRangeMapper usageRangeMapper;

    public UsageRangeServiceImpl(UsageRangeMapper usageRangeMapper) {
        this.usageRangeMapper = usageRangeMapper;
    }

    /**
     * 查询用量区间设置
     *
     * @param tenantId       用量区间设置主键
     * @param warnTargetType 报警对象类型，1：大表、2：小表
     * @return 用量区间设置
     */
    @Override
    public List<UsageRange> selectUsageRangeByTenantId(String tenantId, String warnTargetType) {
        List<UsageRange> usageRanges = usageRangeMapper.selectUsageRangeByTenantId(tenantId, warnTargetType);
        if (CollectionUtils.isEmpty(usageRanges)) {
            return usageRanges;
        }
        Map<String, List<UsageRange>> usageRangeGroupByStrategy = new LinkedHashMap<>(10);
        for (UsageRange usageRange : usageRanges) {
            List<UsageRange> list = usageRangeGroupByStrategy.computeIfAbsent(usageRange.getStrategy(), k -> new ArrayList<>(15));
            list.add(usageRange);
        }
        List<UsageRange> returnList = new ArrayList<>(usageRangeGroupByStrategy.keySet().size());
        for (String key : usageRangeGroupByStrategy.keySet()) {
            List<UsageRange> list = usageRangeGroupByStrategy.get(key);
            List<UsageRange.UsageRangeData> dataList = new ArrayList<>(list.size());
            UsageRange usageRange = list.get(0);
            for (UsageRange range : list) {
                dataList.add(new UsageRange.UsageRangeData(range.getWarnType(), range.getValue(), range.getEnabled()));
            }

            usageRange.setData(dataList);
            returnList.add(usageRange);
        }
        return returnList;
    }

    /**
     * 新增用量区间设置
     *
     * @param usageRange 用量区间设置
     * @return 结果
     */
    @Override
    public int insertUsageRange(UsageRange usageRange) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        usageRange.setCreateBy(user.getUserName());
        usageRange.setUpdateBy(user.getUserName());
        usageRange.setCreateTime(DateUtils.getNowDate());
        List<UsageRange.UsageRangeData> dataList = usageRange.getData();
        List<UsageRange> usageRanges = new ArrayList<>(dataList.size());
        for (UsageRange.UsageRangeData data : dataList) {
            UsageRange range = BeanUtil.copyProperties(usageRange, UsageRange.class, "data");
            range.setWarnType(data.getWarnType());
            range.setValue(data.getValue());
            range.setEnabled(data.getEnabled());
            usageRanges.add(range);
        }
        return usageRangeMapper.insertOrUpdateUsageRangeBatch(usageRanges);
    }

}
